package pl.mchaniewski.ryneczek.services.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pl.mchaniewski.ryneczek.dao.OrderDao;
import pl.mchaniewski.ryneczek.pojo.Company;
import pl.mchaniewski.ryneczek.pojo.Order;
import pl.mchaniewski.ryneczek.pojo.Portfolio;
import pl.mchaniewski.ryneczek.services.OrderService;
import pl.mchaniewski.ryneczek.utilities.DateUtil;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    private static final Logger logger = LoggerFactory
            .getLogger(OrderServiceImpl.class);
    private OrderDao orderDao;

    @Autowired
    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    @Override
    public Order findById(Integer id) {
        try {
            return orderDao.findById(id);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public Order save(Order entity) {
        try {
            return orderDao.save(entity);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public List<Order> findByPortfolioIdAndStatus(Integer portfolioId,
            String status) {
        try {
            return orderDao.findByPortfolioIdAndStatus(portfolioId, status);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public List<Order> findByPortfolioIdCompanyIdAndStatus(Integer portfolioId,
            Integer companyId, String status) {
        try {
            return orderDao.findByStatusPortfolioIdAndCompanyId(portfolioId,
                    companyId, status);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public List<Order> findAllButAcceptedByPortfolioId(Integer portfolioId) {
        try {
            return orderDao.findAllButAcceptedByPortfolioId(portfolioId);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public List<Order> findAllButAcceptedByPortfolioIdAndCompanyId(
            Integer portfolioId, Integer companyId) {
        try {
            return orderDao.findAllButAcceptedByPortfolioIdAndCompanyId(
                    portfolioId, companyId);
        } catch (Exception ex) {
            logger.error("Exception: {}", ex.getMessage());

            return null;
        }
    }

    @Override
    public Order createNewOrder(Portfolio portfolio, Company company,
            String transactionType, BigDecimal originalPrice, int amount,
            BigDecimal setPrice, int priceType, Date expiryDate) {
        Order newOrder = new Order();
        newOrder.setPortfolio(portfolio);
        newOrder.setCompany(company);
        newOrder.setTransactionType(transactionType);
        newOrder.setOriginalPrice(originalPrice);
        newOrder.setAmount(amount);
        newOrder.setSetPrice(setPrice);
        newOrder.setPriceType(priceType);
        newOrder.setCondition(Order.CONDITION_NULL);
        newOrder.setExpiryDate(expiryDate);
        newOrder.setSubmissionDate(DateUtil.getNow());
        newOrder.setStatus(Order.STATUS_ACCEPTED);

        return save(newOrder);
    }

}
